#! /bin/sh

control_script="sh @KEDR_INSTALL_PREFIX_EXEC@/kedr"
kedr_name="@KEDR_CORE_NAME@"
# Name of any payload (empty if shouldn't be tested)
payload_name="@NAME_OF_PAYLOAD_LOADED@"
# Whether module is currently loading
is_module_loading()
{
    @LSMOD@ | grep "^$1[[:blank:]]\{1,\}" > /dev/null
}

# File for store 'kedr status' output.
status_file="status_file"

. ./common.sh

${control_script} start target1
if test $? -ne 0; then
    printf "Error occured when 'start' command was executed\n"
    exit 1
fi

if ! is_module_loading ${kedr_name}; then
    printf "KEDR core module is not loaded after 'start' command\n"
    exit 1
fi

if test "${payload_name}"; then
    if ! is_module_loading ${payload_name}; then
    printf "Payload module is not loaded after 'start' command\n"
        ${control_script} stop
    exit 1
    fi
fi

${control_script} status > ${status_file}
if test $? -ne 0; then
    printf "Error occured when 'status' command was executed after 'start' command\n"
    ${control_script} stop
    exit 1
fi

kedr_status=`get_kedr_status ${status_file}`
if test "$kedr_status" != "loaded"; then
    if test -z "$kedr_status"; then
        printf "Failed to extract status of KEDR from 'kedr status' output\n"
    else
        printf "Status of KEDR should be 'loaded', but it is '%s'\n" "$kedr_status"
    fi
    ${control_script} stop
    exit 1
fi

kedr_target=`get_kedr_target ${status_file}`
if test "$kedr_target" != "target1"; then
    if test -z "$kedr_target"; then
        printf "Failed to extract target name from 'kedr status' output\n"
    else
        printf "Target name should be 'target1', but it is '%s'\n" "$kedr_target"
    fi
    ${control_script} stop
    exit 1
fi

kedr_target_status=`get_kedr_target_status ${status_file}`
if test "$kedr_target_status" != "not loaded"; then
    if test -z "$kedr_target_status"; then
        printf "Failed to extract target status from 'kedr status' output\n"
    else
        printf "Status of target should be 'not loaded', but it is '%s'\n" "$kedr_target_status"
    fi
    ${control_script} stop
    exit 1
fi

${control_script} stop
if test $? -ne 0; then
    printf "Error occured when 'stop' command was executed\n"
    exit 1
fi

${control_script} status > ${status_file}
if test $? -ne 0; then
    printf "Error occured when 'status' command was executed after 'stop' command\n"
    exit 1
fi

kedr_status=`get_kedr_status ${status_file}`
if test "$kedr_status" != "not loaded"; then
    if test -z "$kedr_status"; then
        printf "Failed to extract status of KEDR from 'kedr status' output\n"
    else
        printf "Status of KEDR should be 'not loaded', but it is '%s'\n" "$kedr_status"
    fi
    exit 1
fi

${control_script} start target2
if test $? -ne 0; then
    printf "Error occured when 'start' command was executed again\n"
    exit 1
fi

${control_script} restart
if test $? -ne 0; then
    printf "Error occured when 'restart' command was executed\n"
    exit 1
fi

if ! is_module_loading ${kedr_name}; then
    printf "KEDR module is not loaded after 'restart' command\n"
    exit 1
fi

if test "${payload_name}"; then
    if ! is_module_loading ${payload_name}; then
        printf "Payload module is not loaded after 'restart' command\n"
        ${control_script} stop
        exit 1
    fi
fi

${control_script} stop
if test $? -ne 0; then
    printf "Error occured when 'stop' command was executed after restart\n"
    exit 1
fi

if is_module_loading ${kedr_name}; then
    printf "KEDR core module is loaded after 'stop' command\n"
    exit 1
fi

if is_module_loading ${payload_name}; then
    printf "Payload module is loaded after 'stop' command\n"
    exit 1
fi
